package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CartController {
    public Object listCart(HttpServletRequest request){

        String userid = request.getParameter("userId");
        Integer businessid = Integer.valueOf(request.getParameter("businessId"));
        CartService cartService = new CartServiceImpl();
        List<Cart> list = cartService.slistCart(userid,businessid);
        return list;
    }

    public Object saveCart(HttpServletRequest request){
        Cart cart = new Cart();
        String userid = request.getParameter("userId");
        Integer businessid = Integer.valueOf(request.getParameter("businessId"));
        Integer foodid = Integer.valueOf(request.getParameter("foodId"));
        cart.setUserId(userid);
        cart.setBusinessId(businessid);
        cart.setFoodId(foodid);
        CartService cartService = new CartServiceImpl();
        int isRight = cartService.ssaveCart(cart);
        return isRight;
    }

    public Object updateCart(HttpServletRequest request){
        Cart cart = new Cart();
        String userid = request.getParameter("userId");
        Integer businessid = Integer.valueOf(request.getParameter("businessId"));
        Integer foodid = Integer.valueOf(request.getParameter("foodId"));
        Integer quantity = Integer.valueOf(request.getParameter("quantity"));
        cart.setUserId(userid);
        cart.setBusinessId(businessid);
        cart.setFoodId(foodid);
        cart.setQuantity(quantity);
        CartService cartService = new CartServiceImpl();
        int isRight = cartService.supdateCart(cart);
        return isRight;
    }

    public Object removeCart(HttpServletRequest request){
        Cart cart = new Cart();
        String userid = request.getParameter("userId");
        Integer businessid = Integer.valueOf(request.getParameter("businessId"));
        Integer foodid = Integer.valueOf(request.getParameter("foodId"));

        cart.setUserId(userid);
        if (foodid!=null) {
            cart.setFoodId(foodid);
        }
        cart.setBusinessId(businessid);
        CartService cartService = new CartServiceImpl();
        int isRight = cartService.sremoveCart(cart);
        return isRight;
    }
}
